package com.bao.work.idcard.impl;

import com.bao.work.idcard.People;
import com.bao.work.idcard.PeopleManagement;
import com.bao.work.idcard.enum1.Sex;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author baoye
 * @date 2024-07-31/18:44
 */
public class PeopleManagementImpl implements PeopleManagement {
    static {
        PEOPLES.push(new People("35789521311215457X","老蔡",Sex.男,"武汉"));
        PEOPLES.push(new People("357895210110154578","大蔡",Sex.男,"武丿"));
        PEOPLES.push(new People("357895207102154578","中蔡",Sex.男,"武又"));
        PEOPLES.push(new People("12345678912345678X","小蔡",Sex.男,"武乂"));
    }
    @Override
    public void insertPeople() {
        System.out.println("开始录入公民信息:");
        while(true){
            System.out.print("(1)请输入姓名:");
            String name=INPUT.next();
            System.out.print("(2)请输入身份证号:");
            String cardId=INPUT.next();
            try{
                char[] chars = cardId.toCharArray();
                if(cardId.length()!=18){
                    throw new RuntimeException("身份证长度为18");
                }
                for (char c : chars) {
                    if((c>'9'||c<'0')&&chars[chars.length-1]!='X'){
                        throw new RuntimeException("身份证号只能是数字或末尾为英文字母X");
                    }
                }
            }catch (RuntimeException r){
                System.out.println(r.getMessage());
                continue;
            }
            System.out.print("(3)请输入性别(1:男,0:女):");
            String sexNum=INPUT.next();
            Sex sex= Sex.男;
            if ("0".equals(sexNum)){
                sex=Sex.女;
            }
            System.out.print("(4)请输入地址:");
            String address=INPUT.next();

            PEOPLES.push(new People(cardId,name,sex,address));
            System.out.println("录入成功!\n");
            return;
        }


    }

    @Override
    public void selectPeople() {
        PEOPLES.forEach(System.out::println);
        System.out.println("\n\t按任意键退出....\n");
    }

    @Override
    public void updatePeople() {
        //定义一个临时栈
        Deque<People> tempDeque=new LinkedList<>();
        System.out.println("信息修改");
        System.out.print("请输入需要修改的人的身份证号码:");
        //获取一个身份证号
        String cardId=INPUT.next();
        //一个布尔值,记录查找id的情况
        boolean flag = findPeopleByCardID(cardId, tempDeque);
        if(!flag){
            return;
        }
        //开始修改信息
        System.out.println("开始信息修改:");
        System.out.print("请输入姓名:");
        tempDeque.getFirst().setName(INPUT.next());
        System.out.print("请输入性别(1:男 0:女):");
        tempDeque.getFirst().setSex(Sex.男);
        if("0".equals(INPUT.next())){
            tempDeque.getFirst().setSex(Sex.女);
        }
        System.out.print("请输入地址:");
        tempDeque.getFirst().setAddress(INPUT.next());
        //将临时栈元素返回到默认栈
        while(tempDeque.size()!=0){
            PEOPLES.push(tempDeque.pop());
        }
        System.out.println("修改信息成功!");

    }

    @Override
    public void deletePeople() {
        Deque<People> tempDeque=new LinkedList<>();
        System.out.println("开始删除信息:");
        System.out.print("请输入需要删除的人的身份证号");
        //获取一个身份证号
        String cardId=INPUT.next();
        boolean flag = findPeopleByCardID(cardId, tempDeque);
        if(!flag){
            return;
        }
        //开始修改信息
        System.out.println("正在删除中...\n");
        //删除top元素
        tempDeque.remove();
        while(tempDeque.size()!=0){
            PEOPLES.push(tempDeque.pop());
        }
        System.out.println("删除成功!\n");
    }

    @Override
    public boolean findPeopleByCardID(String cardId, Deque<People> tempDeque) {
        //一个布尔值,记录查找id的情况
        boolean flag=false;
        //将默认栈中的元素移动到临时栈中
        while(true){
            //获取默认栈中top元素到临时栈中
            People temp=PEOPLES.pop();
            tempDeque.push(temp);
            //比较id
            if(tempDeque.getFirst().getCardId().equals(cardId)){
                //找到id一样的元素,停止循环,修改flag
                flag=true;
                break;
            }
            if(PEOPLES.size()==0){
                //如果默认栈空,则代表没有
                break;
            }
        }
        if(!flag){
            //如果没找到且循环结束,则退出,将临时栈元素返回到默认栈
            while(tempDeque.size()!=0){
                PEOPLES.push(tempDeque.pop());
            }
            System.out.println("\n没有找到该身份证信息,即将返回主菜单!\n");
            return false;
        }
        return true;
    }
}
